package com.schematic.flixel.spaceinvaders
{
import org.flixel.FlxSprite;
import org.flixel.FlxG;

/**
 * The laser cannon
 */
public class Cannon extends FlxSprite
{
    //--------------------------------------------------------------------------
    //
    //  Constants
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  CANNON
    //----------------------------------
    [Embed(source="/../assets/png/cannon.png")]
    /**
     * The cannon png
     */
    public static const CANNON:Class;
    
    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new instance of this class.
     */
    public function Cannon()
    {
        super(0, 0, CANNON);
    }

    //--------------------------------------------------------------------------
    //
    //  Overridden Methods - FlxObject
    //
    //--------------------------------------------------------------------------    
    
    override public function update():void
    {
        var v:Number = 400;
        velocity.x = 0;
        velocity.y = 0;
        
        if (FlxG.keys.LEFT)
            velocity.x = -v;
        else if (FlxG.keys.RIGHT)
            velocity.x = v;
            
        super.update();
        
        if (x < width)
            x = width;
        else if (x > FlxG.width - (width * 2))
            x = FlxG.width  - (width * 2);
    }
    
    override public function kill():void
    {
        FlxG.state.add(new Explosion(width / 2 + x, height / 2 + y));
        FlxG.play(Sounds.EXPLOSION_CANNON);
        super.kill();
    }    
    
}
}